package com.evon.yardmanagement.export;

import java.io.File;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import com.evon.yardmanagement.model.KeyBin;
import com.evon.yardmanagement.model.Yard;

/**
 * Exportable and importable data for Key Bin.
 * 
 * @author vgaurav
 * 
 */
public class KeyBinExportableData implements ExportableData, ImportableData {

	// Data to be exported
	private List<KeyBin> keyBinDataList;

	private File fileToImport;

	private static final int COLUMN_YARD_ID = 10;

	private static final int COLUMN_CAPACITY = 9;

	private static final int COLUMN_UPDATED_USER = 8;

	private static final int COLUMN_CREATED_USER = 7;

	private static final int COLUMN_UPDATED_TIME = 6;

	private static final int COLUMN_CREATED_TIME = 5;
	//
	private static final int COLUMN_IS_ACTIVE = 4;
	//
	private static final int COLUMN_OCCUPIED = 3;
	//
	private static final int COLUMN_KEYBIN_NAME = 2;
	//
	private static final int COLUMN_KEYBIN_NUMBER = 1;
	//
	private static final int COLUMN_KEYBIN_ID = 0;

	private static final String YARD_ID = "YARD_ID";

	private static final String CAPACITY = "CAPACITY";

	private static final String HSC_ID = "HSC_ID";

	private static final String HSU_TS = "HSU_TS";

	private static final String HSC_TS = "HSC_TS";
	//
	private static final String ACTIVE = "ACTIVE";
	//
	private static final String OCCUPIED = "OCCUPIED";
	//
	private static final String KEYBIN_NAME = "SECTION_NAME";
	//
	private static final String KEYBIN_NUMBER = "KEYBOX_NUMBER";
	//
	private static final String KEYBN_ID = "KEYBOX_ID";

	private DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");;

	public KeyBinExportableData(List<KeyBin> keyBins) {
		this.keyBinDataList = keyBins;
	}

	public void setValueAt(int row, int column, Object value) {
		if (keyBinDataList == null) {
			keyBinDataList = new ArrayList<KeyBin>();
		}
		if (column > getColumnCount()) {
			throw new IllegalArgumentException(
					"Column size can not be greater than " + getColumnCount());
		}

		if (keyBinDataList.size() - 1 < row) {
			keyBinDataList.add(new KeyBin());
		}
		KeyBin keyBin = keyBinDataList.get(row);
		if (value == null) {
			return;
		}

		switch (column) {
		case COLUMN_KEYBIN_ID:
			// for new Section

			keyBin.setId((Integer) value);

			break;
		case COLUMN_YARD_ID:
			Yard yard = new Yard();
			yard.setId((Integer) value);
			keyBin.setYard(yard);
			break;
		case COLUMN_KEYBIN_NAME:
			keyBin.setName((String) value);
			break;

		case COLUMN_KEYBIN_NUMBER:
			if (value instanceof String)
				keyBin.setBinNumber(Integer.valueOf((String) value));
			else
				keyBin.setBinNumber((Integer) value);
			break;

		case COLUMN_OCCUPIED:
			if (value instanceof String)
				keyBin.setIsOccupied(Integer.valueOf((String) value));
			else
				keyBin.setIsOccupied((Integer) value);
			break;

		case COLUMN_IS_ACTIVE:
			if (value instanceof String)
				keyBin.setIsActive(Integer.valueOf((String) value));
			else
				keyBin.setIsActive((Integer) value);
			break;
		case COLUMN_CREATED_TIME:
			DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
			Date d;
			try {
				d = (Date) formatter.parse((String) value);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				d = new java.util.Date();
			}
			keyBin.setCreatedDate(d);
			break;
		case COLUMN_UPDATED_TIME:
			DateFormat formatter1 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
			Date d1;
			try {
				d1 = (Date) formatter1.parse((String) value);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				d1 = new java.util.Date();
			}
			keyBin.setUpdatedDate(d1);
			break;

		case COLUMN_CREATED_USER:
			keyBin.setCreatedBy((String) value);
			break;

		case COLUMN_UPDATED_USER:
			keyBin.setUpdatedBy((String) value);
			break;

		case COLUMN_CAPACITY:
			if (value instanceof String)
				keyBin.setCapacity(Integer.valueOf((String) value));
			else

				keyBin.setCapacity((Integer) value);
			break;

		default:
			throw new IllegalArgumentException("Column size is not correct- "
					+ column);
		}

	}

	public File getFileToImport() {
		// TODO Auto-generated method stub
		return fileToImport;
	}

	public void setFileToImport(File fileToImport) {
		this.fileToImport = fileToImport;

	}

	public List<KeyBin> getData() {
		if (keyBinDataList == null || keyBinDataList.isEmpty()) {
			return Collections.EMPTY_LIST;
		}
		return keyBinDataList;
	}

	public int getColumnCount() {
		return 11;
	}

	public int getRowCount() {
		if (keyBinDataList == null || keyBinDataList.isEmpty()) {
			return 0;
		}
		return keyBinDataList.size();
	}

	public List<String> getColumnHeader() {
		return Arrays.asList(KEYBN_ID, KEYBIN_NUMBER, KEYBIN_NAME, OCCUPIED,
				ACTIVE, HSC_TS, HSU_TS, HSC_ID, HSC_ID, CAPACITY, YARD_ID);
	}

	public String getValueAt(int row, int column) {
		if (keyBinDataList == null || keyBinDataList.isEmpty()) {
			throw new IllegalStateException(
					"The KeyBinExportableData has no model set, please set the list of model first before calling this method.");
		}
		if (row >= keyBinDataList.size() || column > getColumnCount()) {
			throw new IllegalArgumentException(
					"The row and column range is out of bound. Chcek the row and column size being passed to this method.");
		}

		KeyBin keyBin = keyBinDataList.get(row);

		if (keyBin == null) {
			throw new IllegalStateException(
					"Key Bin inside keyBinDataList is null.");
		}

		switch (column) {
		case COLUMN_KEYBIN_ID:
			return String.valueOf(keyBin.getId());
		case COLUMN_YARD_ID:
			return String.valueOf(keyBin.getYard().getId());
		case COLUMN_KEYBIN_NUMBER:
			return String.valueOf(keyBin.getBinNumber());
		case COLUMN_KEYBIN_NAME:
			return keyBin.getName();
		case COLUMN_IS_ACTIVE:
			return String.valueOf(keyBin.getIsActive());

		case COLUMN_OCCUPIED:
			return String.valueOf(keyBin.getIsOccupied());
		case COLUMN_CREATED_TIME:
			if (keyBin.getCreatedTime() != null)
				return formatter.format(keyBin.getCreatedTime());
			else
				formatter.format(new Date());
		case COLUMN_UPDATED_TIME:
			if (keyBin.getLastUpdatedTime() != null)
				return formatter.format(keyBin.getLastUpdatedTime());
			else
				formatter.format(new Date());
		case COLUMN_CREATED_USER:
			return keyBin.getCreatedUserName();
		case COLUMN_UPDATED_USER:
			return keyBin.getLastUpdatedUserName();
		case COLUMN_CAPACITY:
			return String.valueOf(keyBin.getCapacity());
		default:
			return "";
		}
	}

	public String getDataName() {
		// TODO Auto-generated method stub
		return "KEYBINS";
	}

	public ExportCellType getColumnClass(int columnNumber) {
		// TODO Auto-generated method stub
		return null;
	}

}
